package com.chris.libs.base.app;

import android.content.Context;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.widget.TextView;

import com.chris.libs.http.x.v1.NetManager;

public class LoadingDialog extends BaseDialog {
    //@BindView(R.id.tv_message)
    private TextView mTvMessage;

    //标记 0 - 表示待显示,1 - 表示已经隐藏
    private int flag = 0;

    public LoadingDialog(Context context) {
        super(context);
        this.mTvMessage = this.contentView.findViewById(R.id.tv_message);
    }

    public static LoadingDialog create(Context context) {
        return new LoadingDialog(context);
    }

    public static LoadingDialog show(Context context) {
        LoadingDialog loadingDialog = new LoadingDialog(context);
        loadingDialog.show();
        return loadingDialog;
    }

    public static LoadingDialog show(Context context, final Object tag, String title) {
        LoadingDialog loadingDialog = new LoadingDialog(context);
        loadingDialog.show(tag, title);
        return loadingDialog;
    }

    @Override
    public boolean isCancelable() {
        return true;
    }

    public int getFlag() {
        return flag;
    }

    public void setFlag(int flag) {
        this.flag = flag;
    }

    @Override
    public int layoutId() {
        return R.layout.app_dialog_logining;
    }

    public void show(final Object tag, String title) {
        if (!TextUtils.isEmpty(title)) {
            mTvMessage.setText(title);
            getDialog().setOnCancelListener(new DialogInterface.OnCancelListener() {
                @Override
                public void onCancel(DialogInterface dialogInterface) {
                    NetManager.removeAllRequest();
                }
            });
        }
        show();
    }

    @Override
    public void show() {
        //等3秒
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                try {
//                    Thread.sleep(3000);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//                if (getFlag() == 0) {
//                    super.show();
//                }
//            }
//        }).start();


        super.show();
    }

    @Override
    public void dismiss() {
        super.dismiss();
        setFlag(1);
    }
}
